package gui;

import ge.main.GerenciadorDeEscolas;
import ge.util.Validadores;
import ge.escola.Escola;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;
import javax.swing.border.BevelBorder;
import javax.swing.border.LineBorder;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class PrimeiroAcesso extends javax.swing.JPanel {

	public PrimeiroAcesso() {
		initComponents();

	}

	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

		this.setLayout(null);

		setMinimumSize(new java.awt.Dimension(800, 600));
		setPreferredSize(new java.awt.Dimension(800, 600));
		this.setBackground(new java.awt.Color(204, 204, 255));
		{
			gerenciadorKamalvin = new JLabel();
			this.add(gerenciadorKamalvin);
			gerenciadorKamalvin.setText("Gerenciador De Escolas");
			gerenciadorKamalvin.setBounds(193, 12, 423, 48);
			gerenciadorKamalvin.setFont(new java.awt.Font("Arial", 0, 36));
			gerenciadorKamalvin.setForeground(new java.awt.Color(0, 102, 102));
			gerenciadorKamalvin.setBorder(BorderFactory
					.createEtchedBorder(BevelBorder.LOWERED));
		}
		{
			escolaNome = new JLabel();
			this.add(escolaNome);
			escolaNome.setText("Nome da Escola");
			escolaNome.setBounds(69, 96, 180, 25);
			escolaNome.setFont(new java.awt.Font("Arial", 0, 24));
			escolaNome.setForeground(new java.awt.Color(0, 102, 102));
		}
		{
			endereco = new JLabel();
			this.add(endereco);
			endereco.setText("Endereco");
			endereco.setBounds(138, 133, 111, 31);
			endereco.setFont(new java.awt.Font("Arial", 0, 24));
			endereco.setForeground(new java.awt.Color(0, 102, 102));
		}
		{
			nucleo = new JLabel();
			this.add(nucleo);
			nucleo.setText("Nucleo de Ensino");
			nucleo.setBounds(55, 181, 194, 19);
			nucleo.setFont(new java.awt.Font("Arial", 0, 24));
			nucleo.setForeground(new java.awt.Color(0, 102, 102));
		}
		{
			anoFundacao = new JLabel();
			this.add(anoFundacao);
			anoFundacao.setText("Ano da Fundacao");
			anoFundacao.setBounds(53, 223, 196, 25);
			anoFundacao.setFont(new java.awt.Font("Arial", 0, 24));
			anoFundacao.setForeground(new java.awt.Color(0, 102, 102));
		}
		{
			telefone = new JLabel();
			this.add(telefone);
			telefone.setText("Telefone");
			telefone.setBounds(153, 264, 96, 22);
			telefone.setFont(new java.awt.Font("Arial", 0, 24));
			telefone.setForeground(new java.awt.Color(0, 102, 102));
		}
		{
			email = new JLabel();
			this.add(email);
			email.setText("Email");
			email.setBounds(182, 307, 67, 22);
			email.setFont(new java.awt.Font("Arial", 0, 24));
			email.setForeground(new java.awt.Color(0, 102, 102));
		}
		{
			duracao = new JLabel();
			this.add(duracao);
			duracao.setText("Duracao da aula");
			duracao.setBounds(67, 350, 182, 25);
			duracao.setFont(new java.awt.Font("Arial", 0, 24));
			duracao.setForeground(new java.awt.Color(0, 102, 102));
		}
		{
			campoNome = new JTextField();
			this.add(campoNome);
			campoNome.setBounds(283, 100, 333, 22);
			campoNome.setFont(new java.awt.Font("Arial", 0, 18));
			campoNome.setForeground(new java.awt.Color(192,192,192));
			campoNome.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyReleased(java.awt.event.KeyEvent evt) {
					caixaNomeKeyReleased(evt);
				}
			});
		}
		{
			campoEndereco = new JTextField();
			this.add(campoEndereco);
			campoEndereco.setBounds(283, 142, 333, 22);
			campoEndereco.setFont(new java.awt.Font("Arial", 0, 18));
			campoEndereco.setForeground(new java.awt.Color(192,192,192));
			campoEndereco.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyReleased(java.awt.event.KeyEvent evt) {
					caixaEnderecoKeyReleased(evt);
				}
			});
		}
		{
			campoNucleo = new JTextField();
			this.add(campoNucleo);
			campoNucleo.setBounds(283, 184, 333, 22);
			campoNucleo.setFont(new java.awt.Font("Arial", 0, 18));
			campoNucleo.setForeground(new java.awt.Color(192,192,192));
			campoNucleo.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyReleased(java.awt.event.KeyEvent evt) {
					caixaNucleoKeyReleased(evt);
				}
			});
		}
		{
			campoFundacao = new JTextField();
			this.add(campoFundacao);
			campoFundacao.setBounds(283, 227, 333, 22);
			campoFundacao.setFont(new java.awt.Font("Arial", 0, 18));
			campoFundacao.setForeground(new java.awt.Color(192,192,192));
			campoFundacao.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyReleased(java.awt.event.KeyEvent evt) {
					caixaAnoFundKeyReleased(evt);
				}
			});
		}
		{
			telUm = new JTextField();
			this.add(telUm);
			telUm.setFont(new java.awt.Font("Arial",0,18));
			telUm.setText("XX");
			telUm.setForeground(new java.awt.Color(192,192,192));
			telUm.setBounds(282, 264, 52, 27);
			telUm.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent evt) {
						telUm.setText("");
				}
			});
			telUm.addKeyListener(new KeyAdapter() {
				public void keyReleased(KeyEvent evt) {
					telUmKeyReleased(evt);
				}
			});
		}
		{
			telDois = new JTextField();
			this.add(telDois);
			telDois.setText("XXXX");
			telDois.setBounds(346, 264, 52, 27);
			telDois.setFont(new java.awt.Font("Arial", 0, 18));
			telDois.setForeground(new java.awt.Color(192,192,192));
			telDois.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent evt) {
					telDois.setText("");
			}
		});
			telDois.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyReleased(java.awt.event.KeyEvent evt) {
					caixaTelefoneKeyReleased(evt);
				}
			});
		}
		{
			telTres = new JTextField();
			this.add(telTres);
			telTres.setText("XXXX");
			telTres.setBounds(416, 264, 52, 27);
			telTres.setFont(new java.awt.Font("Arial", 0, 18));
			telTres.setForeground(new java.awt.Color(192,192,192));
			telTres.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent evt) {
					telTres.setText("");
			}
		});
			telTres.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyReleased(java.awt.event.KeyEvent evt) {
					caixaTelefoneKeyReleased(evt);
				}
			});
		}
		{
			campoEmail = new JTextField();
			this.add(campoEmail);
			campoEmail.setBounds(280, 312, 336, 22);
			campoEmail.setFont(new java.awt.Font("Arial", 0, 18));
			campoEmail.setForeground(new java.awt.Color(192,192,192));
			campoEmail.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyReleased(java.awt.event.KeyEvent evt) {
					caixaEmailKeyReleased(evt);
				}
			});
		}
		{
			campoDuracaoUm = new JTextField();
			this.add(campoDuracaoUm);
			campoDuracaoUm.setText("HH");
			campoDuracaoUm.setBounds(280, 352, 35, 26);
			campoDuracaoUm.setFont(new java.awt.Font("Arial", 0, 18));
			campoDuracaoUm.setForeground(new java.awt.Color(192,192,192));
			campoDuracaoUm.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent evt) {
					campoDuracaoUm.setText("");
			}
		});
			campoDuracaoUm.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyReleased(java.awt.event.KeyEvent evt) {
					caixaDuracaoKeyReleased(evt);
				}
			});
		}
		{
			campoDoisPontos = new JLabel();
			this.add(campoDoisPontos);
			campoDoisPontos.setText(":");
			campoDoisPontos.setBounds(327, 353, 10, 19);
			campoDoisPontos.setFont(new java.awt.Font("Arial", 0, 24));
		}
		{
			campoDuracaoDois = new JTextField();
			this.add(campoDuracaoDois);
			campoDuracaoDois.setText("MM");
			campoDuracaoDois.setBounds(344, 352, 37, 27);
			campoDuracaoDois.setFont(new java.awt.Font("Arial", 0, 18));
			campoDuracaoDois.setForeground(new java.awt.Color(192,192,192));
			campoDuracaoDois.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent evt) {
					campoDuracaoDois.setText("");
			}
		});
			campoDuracaoDois.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyReleased(java.awt.event.KeyEvent evt) {
					caixaDuracaoKeyReleased(evt);
				}
			});
		}
		{
			butaoCadastrar = new JButton();
			this.add(butaoCadastrar);
			butaoCadastrar.setText("Cadastrar");
			butaoCadastrar.setBounds(157, 464, 131, 23);
			butaoCadastrar.setFont(new java.awt.Font("Arial", 2, 18));
			butaoCadastrar.setBackground(new java.awt.Color(255, 255, 255));
			butaoCadastrar.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent evt) {
					try {
						cadastrarButtonMouseClicked(evt);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}
		{
			botaoLimpar = new JButton();
			this.add(botaoLimpar);
			botaoLimpar.setText("Limpar");
			botaoLimpar.setBounds(315, 463, 131, 24);
			botaoLimpar.setFont(new java.awt.Font("Arial", 2, 18));
			botaoLimpar.setBackground(new java.awt.Color(255, 255, 255));
			botaoLimpar.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent evt) {
					try {
						limparButtonMouseClicked(evt);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}
		{
			botaoCancelar = new JButton();
			this.add(botaoCancelar);
			botaoCancelar.setText("Cancelar");
			botaoCancelar.setBounds(475, 463, 131, 24);
			botaoCancelar.setFont(new java.awt.Font("Arial", 2, 18));
			botaoCancelar.setBackground(new java.awt.Color(255, 255, 255));
			botaoCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
				public void mouseClicked(java.awt.event.MouseEvent evt) {
					try {
						cancelarButtonMouseClicked(evt);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}
		{
			checaNome = new JLabel();
			this.add(checaNome);
			checaNome.setBounds(651, 101, 63, 19);
			checaNome.setFont(new java.awt.Font("Arial", 0, 18));
		}
		{
			checaEndereco = new JLabel();
			this.add(checaEndereco);
			checaEndereco.setBounds(651, 146, 63, 15);
			checaEndereco.setFont(new java.awt.Font("Arial", 0, 18));
		}
		{
			checaNucleo = new JLabel();
			this.add(checaNucleo);
			checaNucleo.setBounds(651, 188, 63, 15);
			checaNucleo.setFont(new java.awt.Font("Arial", 0, 18));
		}
		{
			checaFundacao = new JLabel();
			this.add(checaFundacao);
			checaFundacao.setBounds(651, 231, 63, 15);
			checaFundacao.setFont(new java.awt.Font("Arial", 0, 18));
		}
		{
			checaTelefone = new JLabel();
			this.add(checaTelefone);
			checaTelefone.setBounds(486, 270, 64, 15);
			checaTelefone.setFont(new java.awt.Font("Arial", 0, 18));
		}
		{
			checaEmail = new JLabel();
			this.add(checaEmail);
			checaEmail.setBounds(651, 315, 63, 15);
			checaEmail.setFont(new java.awt.Font("Arial", 0, 18));
		}
		{
			checaDuracao = new JLabel();
			this.add(checaDuracao);
			checaDuracao.setBounds(408, 359, 64, 15);
			checaDuracao.setFont(new java.awt.Font("Arial", 0, 18));
		}
		{
			caixaMaior = new JLabel();
			this.add(caixaMaior);
			caixaMaior.setBounds(19, 69, 767, 372);
			caixaMaior.setBorder(BorderFactory.createEtchedBorder(BevelBorder.LOWERED));
		}

	}// </editor-fold>

	private void caixaNomeKeyReleased(java.awt.event.KeyEvent evt) {
		String nome = campoNome.getText();
		if (!Validadores.validaNome(nome)) {
			checaNome.setForeground(Color.RED);
			checaNome.setText("Invalido");
			nomeValido = false;
		} else {
			checaNome.setForeground(Color.GREEN);
			checaNome.setText("Ok");
			nomeValido = true;
		}
	}

	private void caixaAnoFundKeyReleased(java.awt.event.KeyEvent evt) {
		String anoFund = campoFundacao.getText();
		if (!Validadores.validaAno(anoFund)) {
			checaFundacao.setForeground(Color.RED);
			checaFundacao.setText("Invalido");
			anoFundValido = false;
		} else {
			checaFundacao.setForeground(Color.GREEN);
			checaFundacao.setText("Ok");
			anoFundValido = true;
		}
	}

	private void caixaEnderecoKeyReleased(java.awt.event.KeyEvent evt) {
		String endereco = campoEndereco.getText();
		if (!Validadores.validaEndereco(endereco)) {
			checaEndereco.setForeground(Color.RED);
			checaEndereco.setText("Invalido");
			enderecoValido = false;
		} else {
			enderecoValido = true;
			checaEndereco.setForeground(Color.GREEN);
			checaEndereco.setText("Ok");
		}
	}

	private void caixaNucleoKeyReleased(java.awt.event.KeyEvent evt) {
		String nucleo = campoNucleo.getText();
		if (!Validadores.validaNome(nucleo)) {
			nucleoValido = false;
			checaNucleo.setForeground(Color.RED);
			checaNucleo.setText("Invalido");
		} else {
			checaNucleo.setForeground(Color.GREEN);
			checaNucleo.setText("Ok");
			nucleoValido = true;
		}
	}

	private void caixaTelefoneKeyReleased(java.awt.event.KeyEvent evt) {
		String DDD = telUm.getText();
		String num1 = telDois.getText();
		String num2 = telTres.getText();

		if (!Validadores.validaTelefone("(" + DDD + ")" + num1 + "-" + num2)) {
			checaTelefone.setText("Invalido");
			checaTelefone.setForeground(Color.RED);
			telefoneValido = false;
		} else {
			checaTelefone.setForeground(Color.GREEN);
			checaTelefone.setText("Ok");
			telefoneValido = true;
		}
	}

	private void caixaEmailKeyReleased(java.awt.event.KeyEvent evt) {
		String email = campoEmail.getText();
		if (!Validadores.validaEmail(email)) {
			emailValido = false;
			checaEmail.setForeground(Color.RED);
			;
			checaEmail.setText("Invalido");
		} else {
			checaEmail.setForeground(Color.GREEN);
			;
			checaEmail.setText("Ok");
			emailValido = true;
		}
	}

	private void caixaDuracaoKeyReleased(java.awt.event.KeyEvent evt) {
		String duracao = campoDuracaoUm.getText() + ":"
				+ campoDuracaoDois.getText();
		if (!Validadores.validaHora(duracao)) {
			checaDuracao.setText("Invalido");
			checaDuracao.setForeground(Color.RED);
			duracaoValido = false;
		} else {
			checaDuracao.setForeground(Color.GREEN);
			checaDuracao.setText("Ok");
			duracaoValido = true;
		}
	}

	private void cadastrarButtonMouseClicked(java.awt.event.MouseEvent evt)
			throws Exception {
		if (nomeValido && anoFundValido && enderecoValido && nucleoValido
				&& telefoneValido && emailValido && duracaoValido) {
			// cria escola
			GerenciadorDeJanelas.escola = new Escola(campoNome.getText(),
					campoEndereco.getText(), campoNucleo.getText(),
					campoFundacao.getText(), "(" + telUm.getText() + ")"
							+ telDois.getText() + "-" + telTres.getText(),
					campoEmail.getText(), campoDuracaoUm.getText() + ":"
							+ campoDuracaoDois.getText());
			// grava escola criada em um xml
			GerenciadorDeJanelas.gravar(GerenciadorDeJanelas.arquivoEscola,
					GerenciadorDeJanelas.ARQUIVADOR
							.toXML(GerenciadorDeJanelas.escola));
			// vai para proxima janela
			GerenciadorDeJanelas.window.setVisible(false);
			GerenciadorDeJanelas.window
					.setContentPane(GerenciadorDeJanelas.primeiroMenu);
			GerenciadorDeJanelas.window.setVisible(true);
		} else {
			JOptionPane
					.showMessageDialog(
							null,
							"Informa��es da escola n�o cadastradas. Por favor, cheque novamente as informa��es digitadas.");
		}

	}

	private void cancelarButtonMouseClicked(java.awt.event.MouseEvent evt) {
		System.exit(0);
	}

	private void limparButtonMouseClicked(java.awt.event.MouseEvent evt) {
		checaFundacao.setText("");
		checaTelefone.setText("");
		checaNome.setText("");
		checaEmail.setText("");
		checaEndereco.setText("");
		checaNucleo.setText("");
		checaDuracao.setText("");
		campoNome.setText("");
		campoEndereco.setText("");
		campoFundacao.setText("");
		campoNucleo.setText("");
		telUm.setText("XX");
		telDois.setText("XXXX");
		telTres.setText("XXXX");
		campoEmail.setText("");
		campoDuracaoUm.setText("HH");
		campoDuracaoDois.setText("MM");
		nomeValido = false;
		anoFundValido = false;
		enderecoValido = false;
		nucleoValido = false;
		telefoneValido = false;
		emailValido = false;
		duracaoValido = false;

	}
	
	private void telUmKeyReleased(KeyEvent evt) {
		
		
	}

	// Variables declaration - do not modify
	private boolean nomeValido = false;
	private boolean anoFundValido = false;
	private boolean enderecoValido = false;
	private boolean nucleoValido = false;
	private boolean telefoneValido = false;
	private boolean emailValido = false;
	private JLabel escolaNome;
	private JTextField telUm;
	private JLabel caixaMaior;
	private JLabel checaDuracao;
	private JLabel checaEmail;
	private JLabel checaTelefone;
	private JLabel checaFundacao;
	private JLabel checaNucleo;
	private JLabel checaEndereco;
	private JLabel checaNome;
	private JButton botaoCancelar;
	private JButton botaoLimpar;
	private JButton butaoCadastrar;
	private JTextField campoDuracaoDois;
	private JLabel campoDoisPontos;
	private JTextField campoDuracaoUm;
	private JTextField campoEmail;
	private JTextField telTres;
	private JTextField telDois;
	private JTextField campoFundacao;
	private JTextField campoNucleo;
	private JTextField campoEndereco;
	private JTextField campoNome;
	private JLabel duracao;
	private JLabel email;
	private JLabel telefone;
	private JLabel anoFundacao;
	private JLabel nucleo;
	private JLabel endereco;
	private JLabel gerenciadorKamalvin;
	private boolean duracaoValido = false;
	// End of variables declaration
}
